# -*- coding: utf-8 -*-
from sqlalchemy import \
    Table, Column, Integer, String, MetaData, ForeignKey, \
    Index, UniqueConstraint, ForeignKeyConstraint
from sqlalchemy.orm import \
    mapper, relation

metadata = MetaData()

domains_table = Table('domains', metadata,
    Column('id', Integer, primary_key=True, autoincrement=True),
    Column('name', String(255), nullable=False),
    Column('master', String(20), default=None, nullable=True),
    Column('last_check', Integer, default=None, nullable=True),
    Column('type', String(6), nullable=False),
    Column('notified_serial', Integer, default=None, nullable=True),
    Column('account', String(40), default=None, nullable=True),
    UniqueConstraint('name', name='name_index'),
    mysql_engine='InnoDB'
)

records_table = Table('records', metadata,
    Column('id', Integer, primary_key=True, autoincrement=True),
    Column('domain_id', Integer, default=None, nullable=True),
    Column('name', String(255), default=None, nullable=True),
    Column('type', String(6), default=None, nullable=True),
    Column('content', String(255), default=None, nullable=True),
    Column('ttl', Integer, default=None, nullable=True),
    Column('prio', Integer, default=None, nullable=True),
    Column('change_date', Integer, default=None, nullable=True),
    ForeignKeyConstraint(['domain_id'], ['domains.id'], ondelete='CASCADE'),
    mysql_engine='InnoDB'
)

Index('rec_name_index', records_table.c.name)
Index('nametype_index', records_table.c.name, records_table.c.type)
Index('domain_id', records_table.c.domain_id)

supermasters_table = Table('supermasters', metadata,
    Column('ip', String(25), nullable=False),
    Column('nameserver', String(255), nullable=False),
    Column('account', String(40), default=None, nullable=True),
    mysql_engine='InnoDB'
)

class Domain(object):
    def __init__(self):
        pass

class Record(object):
    def __init__(self):
        pass

mapper(Domain, domains_table, properties={
    'records': relation(Record,
                        cascade='all, delete-orphan', passive_deletes=True,
                        lazy='dynamic', backref='domain')
})
mapper(Record, records_table)
